#include <sys/stat.h>
#include <errno.h>

#include <linux/kernel.h>
#include <linux/sched.h>

extern int rw_char(int rw, int dev, char *buf, int count, off_t *pos);
extern int file_read(struct m_inode *inode, struct file *filp, char *buf, int count);
extern int file_write(struct m_inode *inode, struct file *filp, char *buf, int count);

int sys_lseek(unsigned int fd, off_t offset, int origin)
{
    struct file *file;
    int tmp;

    if (fd >= NR_OPEN)
        return -EBADF;
    file = current->filp[fd];
    if (!(file) || !(file->f_inode) || !IS_SEEKABLE(MAJOR(file->f_inode->i_dev)))
        return -EBADF;
    switch (origin) {
        case 0:
            if (offset < 0)
                return -EINVAL;
            file->f_pos = offset;
            break;
        case 1:
            if (file->f_pos + offset < 0)
                return -EINVAL;
            file->f_pos += offset;
            break;
        case 2:
            tmp = file->f_inode->i_size + offset;
            if (tmp < 0)
                return -EINVAL;
            file->f_pos = tmp;
            break;
        default:
            return -EINVAL;
    }
    return file->f_pos;
}

int sys_read(unsigned int fd, char *buf, int count)
{
    struct file *file;
    struct m_inode *inode;

    if (fd >= NR_OPEN || count < 0)
        return -EINVAL;
    file = current->filp[fd];
    if (!file) {
        return -EINVAL;
    }
    if (!count)
        return 0;
    
    verify_area(buf, count);
    inode = file->f_inode;
    if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode)) {
        if (count + file->f_pos > inode->i_size)
            count = inode->i_size - file->f_pos;
        if (count <= 0)
            return 0;
        return file_read(inode, file, buf, count);
    }
    printk("(Read)inode->i_mode=%06o\n\r", inode->i_mode);
    return -EINVAL;
}

int sys_write(unsigned int fd, char *buf, int count)
{
    struct file *file;
    struct m_inode *inode;

    file = current->filp[fd];
    if (fd >= NR_OPEN || count < 0 || !file)
        return -EINVAL;
    if (!count)
        return 0;
    
    inode = file->f_inode;
    if (S_ISCHR(inode->i_mode)) {   // 字符设备文件
        return rw_char(WRITE, inode->i_zone[0], buf, count, &file->f_pos);
    }
    if (S_ISREG(inode->i_mode)) {   // 普通文件
        return file_write(inode, file, buf, count);
    }
    
    printk("(Write)inode->i_mode=%06o\n", inode->i_mode);
    return -EINVAL;
}
